<?php

class DefaultController extends Controller {

    public function actionIndex() {
        $this->render('index');
    }

    public function actionDSYTuong() {
        $YTuongs = YTuongService::getYTuongs();
        $this->render('DSYTuong', array('YTuongs' => $YTuongs));
    }

    public function actionNguoiDung() {
        $Users = NguoiDungService::getAll();
        $this->render('NguoiDung', array('NguoiDungs' => $Users));
    }

    public function actionThamSo() {
        $this->render('ThamSo', array('ThamSos' => ThamSoService::getAll()));
    }

    public function actionCTYTuong() {
        $YTuong = new YTuong();
        $Id = Yii::app()->request->getParam('Id');
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'edit-idea-form') {
            echo CActiveForm::validate($YTuong);
            Yii::app()->end();
        }
        if (isset($_POST['YTuong'])) {
            $YTuong = YTuongService::getYTuongById($Id);
//            $YTuong->attributes = $_POST['YTuong'];
//            $YTuong->Id = $Id;
            $YTuong->TieuDe = $_POST['YTuong']['TieuDe'];
            $YTuong->GioiThieu = $_POST['YTuong']['GioiThieu'];
            $YTuong->Nguon = $_POST['YTuong']['Nguon'];
            $YTuong->NoiDung = $_POST['YTuong']['NoiDung'];
            $imageUploadFile = CUploadedFile::getInstance($YTuong, 'AnhBia');
//            echo var_dump($YTuong);die();
            // validate user input and redirect to the previous page if valid
            if ($YTuong->validate(array('TieuDe', 'GioiThieu', 'Nguon', 'AnhBia', 'NoiDung'))) {

                if ($imageUploadFile !== null) { // validate to save file
                    $imageFileName = $YTuong->Id . '.' . end(explode(".", $imageUploadFile->name));
                    $YTuong->AnhBia = $imageFileName;
                    $imageUploadFile->saveAs(Yii::getPathOfAlias('webroot') . '/images/ArticleCover/' . $imageFileName);
                    $YTuong->update(); //ByPk($Id, array('TieuDe', 'GioiThieu', 'Nguon', 'AnhBia', 'NoiDung'));
                } else {
                    $YTuong->update(); //ByPk($Id, array('TieuDe', 'GioiThieu', 'Nguon', 'NoiDung'));
                }
                $this->redirect(array("DSYTuong"));
            }
        }
        $YT = YTuongService::getYTuongById($Id);
        $this->render('AjaxResponse/CTYTuong', array('YTuong' => $YT));
    }

    public function actionDeleteYTuong() {
        $Id = Yii::app()->request->getParam('Id');
        if (YTuongService::removeYTuongById($Id)) {
            echo 'success';
        } else {
            echo 'fail';
        }
    }

    public function actionShowOrHideYTuong() {
        $Id = Yii::app()->request->getParam('Id');
        $class = '';
        YTuongService::ToggleSeeYTuong($Id) == 0 ? $class = 'fa-icon-eye-open' : $class = 'fa-icon-eye-close';
        echo json_encode(array('status' => 'success', 'class' => $class));
    }

    public function actionLinhVuc() {
        $LV = LinhVuc::model()->findAll();
        $this->render('LinhVuc', array('LinhVucs' => $LV));
    }

    public function actionGetDanhMuc() {
        $Id = Yii::app()->request->getParam('Id');
        $TenLinhVuc = LinhVuc_DanhMucService::getTenLinhVucById($Id);
        $DanhMucs = LinhVuc_DanhMucService::getDanhMucsByLinhVucId($Id);
        echo $this->renderPartial('AjaxResponse/DanhMucBiz', array('DanhMucs' => $DanhMucs, 'TenLV' => $TenLinhVuc), true);
    }

    public function actionEditLinhVuc() {
        $Name = Yii::app()->request->getParam('value');
        $Id = Yii::app()->request->getParam('pk');
        if (LinhVuc_DanhMucService::changeTenLinhVuc($Id, $Name)) {
            echo 'success';
        } else {
            echo 'fail';
        }
    }

    public function actionEditDanhMuc() {
        $Name = Yii::app()->request->getParam('value');
        $Id = Yii::app()->request->getParam('pk');
        if (LinhVuc_DanhMucService::changeTenDanhMuc($Id, $Name)) {
            echo 'success';
        } else {
            echo 'fail';
        }
    }

    public function actionEditThamSo() {
        $Id = Yii::app()->request->getParam('pk');
        $name = Yii::app()->request->getParam('name');
        $value = Yii::app()->request->getParam('value');
        echo ThamSoService::update($Id, array($name => $value));
    }

    public function actionDeleteLinhVuc() {
        $Id = Yii::app()->request->getParam('Id');
        if (LinhVuc_DanhMucService::deleteLinhVuc($Id)) {
            echo 'success';
        } else {
            echo 'Lĩnh vực đã được sử dụng, không thể xóa.';
        }
    }

    public function actionDeleteDanhMuc() {
        $Id = Yii::app()->request->getParam('Id');
        if (LinhVuc_DanhMucService::deleteDanhMuc($Id)) {
            echo 'success';
        } else {
            echo 'Danh mục đã được sử dụng, không thể xóa.';
        }
    }

}
